package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private ITravelCommentService commentService;




    /**
     * 注册请求
     * @param user
     * @return
     */
    @PostMapping
    public JsonResult register(User user){
        JsonResult result = new JsonResult();
        try {
            userService.save(user);
            UserContext.setCurrentUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping
    public JsonResult update(User user){
        JsonResult result = new JsonResult();
        userService.update(user);
        return result.setResult(user);
    }

    @GetMapping("/{authorId}/travels")
    public PageInfo<Travel> list(TravelQueryObject qo){
        return travelService.listByAuthorId(qo);
    }

    /**
     * 新增攻略评论
     * @param strategyComment
     * @return
     */
    @PostMapping("/{user.id}/strategyComments")
    public JsonResult addComment(StrategyComment strategyComment){
        strategyCommentService.save(strategyComment);
        return new JsonResult();
    }

    /**
     * 充值
     * @param user
     * @return
     */
    @PostMapping("/save")
    public JsonResult saveBalance(User user){
        userService.updateBalance(user);
        return new JsonResult();
    }

    @GetMapping("/{id}/save")
    public JsonResult getById(@PathVariable Long id){
        User user = userService.getById(id);
        UserContext.setCurrentUser(user);
        return new JsonResult().setResult(user);
    }
    //获取用户信息
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getAuthorById(@PathVariable Long id) {
        return userService.getAuthorById(id);
    }

    //获取点评信息
    @RequestMapping(value = "/{userId}/commends", method = RequestMethod.GET)
    public List<StrategyComment> getStrategyByUserId(@PathVariable Long userId) {
        return strategyCommentService.getCommentByUserId(userId);
    }


    //根据UserId查询关注数量,和粉丝数量
    @RequestMapping(value = "/{id}/notices", method = RequestMethod.GET)
    public List<Integer> getNoticesById(@PathVariable Long id) {
        //查询该id关注了多少人
        Integer noticedNum = noticeService.getNoticeNum(id);
        //查询该id被多少人关注
        Integer noticeNum = noticeService.getNoticedNum(id);

        return Arrays.asList(noticeNum, noticedNum);
    }

    //根据authorId查询游记数量
    @RequestMapping(value = "/{authorId}/travelsNum", method = RequestMethod.GET)
    public Long getTravelsById(TravelQueryObject qo) {

        qo.setIsPublic(true);
        qo.setState(2);
        return travelService.getTravelsNum(qo);

    }

    /**
     * 提现
     * @param money
     * @param password
     * @return
     */
    @PostMapping("/withdraw")
    public JsonResult withdraw(BigDecimal money,String password){
        JsonResult result = new JsonResult();
        User user = UserContext.getCurrentUser();
        if (!user.getPassword().equals(password)){
            result.mark("登录密码错误");
            return result;
        }
        if (money.compareTo(user.getBalance()) == 1){
            result.mark("余额不足，无法提现");
            return result;
        }
        user.setBalance(user.getBalance().subtract(money));
        userService.updateBalance(user);
        return result;
    }

    @PostMapping("/transfer")
    public JsonResult transfer(BigDecimal money,String password,String email){
        JsonResult result = new JsonResult();
        User user = UserContext.getCurrentUser();
        if (!user.getPassword().equals(password)){
            result.mark("登录密码错误");
            return result;
        }
        if(userService.getByEmail(email) == null){
            result.mark("没有该用户");
            return result;
        }
        if (money == new BigDecimal(0)){
            result.mark("转账金额不规范");
            return result;
        }
        if (money.compareTo(user.getBalance()) == 1){
            result.mark("余额不足，无法转账");
            return result;
        }
        user.setBalance(user.getBalance().subtract(money));
        userService.updateBalance(user);
        User transferUser = userService.login(email, null);
        transferUser.setBalance(transferUser.getBalance().add(money));
        userService.updateBalance(transferUser);
        return result;
    }


    //根据id查询出所有关注人的信息
    @GetMapping("/{userId}/getCares")
    public JsonResult getCares(@PathVariable Long userId){
        List<User> list = userService.getCares(userId);
        return new JsonResult().setResult(list);
    }

    //根据id查询出所有粉丝的信息
    @GetMapping("/{userId}/getFans")
    public JsonResult getFans(@PathVariable Long userId){
        List<User> list = userService.getFans(userId);
        return new JsonResult().setResult(list);
    }

    @GetMapping("/{userId}/travelComments")
    public JsonResult getTravelCommentsByUserId(@PathVariable Long userId){
        List<TravelComment> list = commentService.getTravelCommentsByUserId(userId);
        return new JsonResult().setResult(list);
    }
}
